package com.weixing.mall.provider.controller.admin;

import com.weixing.mall.base.wrapper.Result;
import com.weixing.mall.base.wrapper.ResultUtil;
import com.weixing.mall.core.annotation.NoRepeatSubmit;
import com.weixing.mall.core.support.BaseController;
import com.weixing.mall.provider.model.domain.SpecValue;
import com.weixing.mall.provider.model.dto.SpecValueDto;
import com.weixing.mall.provider.model.query.SpecValueQuery;
import com.weixing.mall.provider.model.vo.SpecValueVo;
import com.weixing.mall.provider.service.ISpecValueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * @ClassName SpecValueController
 * @Description TODO <br/>
 * @Author gjq
 * @Modified By:
 * @Date 2019/8/12
 * @Version 1.0
 **/
@RestController
@RequestMapping("specValue")
@Api(value = "商品规格值APi", tags = "商品规格值")
public class SpecValueController extends BaseController {

    @Autowired
    private ISpecValueService specValueService;


    @ApiOperation(value = "加载颜色体系或者尺码组的规格值")
    @GetMapping("")
    public Result list(SpecValueQuery specValueQuery) {
        return ResultUtil.success(specValueService.selectListByLevel(initMpPage(),specValueQuery));
    }
    @ApiOperation(value = "加载颜色体系或者尺码组的规格值不分页")
    @GetMapping("/all")
    public Result listAll(SpecValueQuery specValueQuery) {
        return ResultUtil.success(specValueService.selectListByLevel(specValueQuery));
    }
    @ApiOperation(value = "加载颜色规格值")
    @GetMapping("/color")
    public Result listColor(SpecValueQuery specValueQuery) {
        return ResultUtil.success(specValueService.selectList(initMpPage(),specValueQuery));
    }

    @ApiOperation(value = "加载尺码规格值")
    @GetMapping("/size")
    public Result listSize(SpecValueQuery specValueQuery) {
        return ResultUtil.success(specValueService.selectList(initMpPage(),specValueQuery));
    }

    @ApiOperation(value = "规格值结构树")
    @GetMapping("/treeList")
    public Result listTree() {
        return ResultUtil.success(specValueService.listTree());
    }

    @ApiOperation(value = "可用的规格值结构树")
    @GetMapping("/treeAbleList/{specId}")
    public Result listAbleList(@PathVariable Long specId) {
        return ResultUtil.success(specValueService.treeAbleList(specId));
    }

    @ApiOperation(value = "保存规格值")
    @PostMapping("save")
    @NoRepeatSubmit
    public Result save(@Validated SpecValueDto specValueDto) {
      /*  SpecValue specValue=   specValueService.getOne(new QueryWrapper<SpecValue>().eq("spec_value",specValueDto.getSpecValue()).eq("spec_level",specValueDto.getSpecLevel()));
        if(specValueDto.getId()==null&&specValue!=null){
            return ResultUtil.error("规格值已经存在请重新填写");
        }*/
        specValueService.save(specValueDto);
        return ResultUtil.success();
    }

    @ApiOperation(value = "删除规格值")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "规格值id", paramType = "path", dataType = "long")
    })
    @PostMapping("del/{id}")
    public Result delById(@PathVariable long id) {
       SpecValue value= specValueService.getById(id);
        if(value.getSpecLevel().toString().equals("1")){//为第一层级
           SpecValueQuery specValueQuery=new SpecValueQuery();
           specValueQuery.setParentId(id);
           List<SpecValueVo> list= specValueService.selectListByLevel(specValueQuery);
          if(list.size()!=0){
             return ResultUtil.error("该色系或者尺码组有下级分类，先删除子级分类");
          }
       }
        return specValueService.removeById(id) ? ResultUtil.success() : ResultUtil.error();
    }
    /*
    * 使用removeByIds存在qs notFound 问题，规避一下
    * */
    @ApiOperation(value = "批量规格值")
    @PostMapping("delByIds/{ids}")
    public Result delByIds(@PathVariable  Long []ids) {
        for(int i=0;i<ids.length;i++){
            SpecValue value= specValueService.getById(ids[i]);
            System.out.println(value.getSpecLevel().toString().equals("1"));
            System.out.println(value.getSpecLevel().equals(1));
            if(value.getSpecLevel().toString().equals("1")){//为第一层级
                SpecValueQuery specValueQuery=new SpecValueQuery();
                specValueQuery.setParentId(ids[i]);
                List<SpecValueVo> list= specValueService.selectListByLevel(specValueQuery);
                if(list.size()!=0){
                    return ResultUtil.error("该色系或者尺码组有下级分类，先删除子级分类");
                }
            }
            specValueService.removeById(ids[i]);
        }
        return   ResultUtil.success();

    }
    @ApiOperation(value = "可用的规格值结构树")
    @GetMapping("/updateChang")
    public Result updateChang(SpecValueQuery specValueQuery) {
        SpecValue specValue=new SpecValue();
        specValue.setId(specValueQuery.getId());
        specValue.setIsEnable(specValueQuery.getIsEnable().getValue());
        specValueService.updateById(specValue);
        return ResultUtil.success();
    }

    @ApiOperation(value = "图片库上传图片 flag  为0 根据文件名称，新建目录。为1时，不新建目录")
    @PostMapping("/init")
    public Result upload(@RequestParam("file") MultipartFile file){
        specValueService.init(file);
        return ResultUtil.success();
    }

}
